<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>打赏推送</title>
</head>
<body>
    <div>
        <input type="button" value="Start" onclick="start()"/>

    </div>
    <div id="messages"></div>
    <script type="text/javascript">
        var ip = window.location.host;
        var url = "ws://"+ip+"/yearmeeting/program/tip";
        var webSocket =
                new WebSocket(url);
        webSocket.onerror = function (event) {
            onError(event)
        };

        webSocket.onopen = function (event) {
            onOpen(event)
        };
        webSocket.onclose = function (event) {
            onClose(event)
        };
        webSocket.onmessage = function (event) {
            onMessage(event)
        };
        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            closeWebSocket();
        }
        //关闭WebSocket连接
        function closeWebSocket() {
            websocket.close();
        }
        function onMessage(event) {
            document.getElementById('messages').innerHTML
                    += '<br />' + event.data;
        }

        function onOpen(event) {
            document.getElementById('messages').innerHTML
                    = 'Connection established';
        }

        function onError(event) {
            alert(event.data);
        }

        function start() {
            webSocket.send('hello');
            return false;
        }

    </script>
</body>
</html>
